<template>
  <view class="loginPage">
    <view class="content header">
      <image class="logo" src="../../static/logo.png"></image>
      <view>
        <text class="title">{{ title }}</text>
      </view>
    </view>
    <view class="content container">
      <uni-card>
        <form @submit="formSubmit">
          <view class="item">
            <view class="title">账 户：</view>
            <input class="uni-input" name="userAccount" v-model="form.userAccount" placeholder="请输入用户名"/>
          </view>
          <view class="item">
            <view class="title">密 码：</view>
            <input type="password" class="uni-input" name="userPassword" v-model="form.userPassword"
                   placeholder="请输入密码"/>
          </view>
          <view class="uni-btn-v item">
            <button type="warn" form-type="submit">登录</button>
            <button type="primary" @click="backHandle">返回</button>
          </view>
        </form>
      </uni-card>
    </view>
  </view>
</template>

<script lang="ts">
import Vue from 'vue';
import {userLoginUsingPost} from "@/servers/api/userController";
import {LOGIN_EVENT, USER_TOKEN_KEY} from "@/constants/sysConstants";

export default Vue.extend({
  data() {
    return {
      title: '用户登录',
      form: <API.UserLoginRequest>{
        userAccount: '',
        userPassword: ''
      }
    }
  },
  async onLoad() {

  },
  methods: {
    formSubmit() {
      userLoginUsingPost(this.form).then(response  => {
        if (response == null) {
          return;
        }
        /*        uni.setStorage({
                  key: USER_TOKEN_KEY,
                  data: response?.id,
                  success: function () {
                    console.log(USER_TOKEN_KEY + 'cache success！');
                  }
                });*/
        // @ts-ignore
        uni.setStorageSync(USER_TOKEN_KEY, response.userAccount);
        this.backHandle()
      })
    },
    backHandle() {
      //触发事件
      uni.$emit(LOGIN_EVENT)
      uni.navigateBack({
        delta: 1
      });
    }
  }
});
</script>

<style>
.loginPage {

}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin: 200rpx auto 50rpx auto;
}

.header {
  margin-top: -50px;
}

.container {
  margin-top: 10rch;
}

.container .item {
  margin: 15px 0 0 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;

  .title {
    color: #171212;
    font-size: 16px;
    margin: 5px 0;
  }
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}

.uni-form-item .title {
  padding: 20rpx 0;
}
</style>
